#include "customScreens.h"

//buttons--------------------
//load up the map screen
class ngb : public eventHandler {
public:
    bool onEvent(SDL_Event* e) {
        if (e->type == SDL_MOUSEBUTTONDOWN) {
            controll::getScrManager()->deactivateScreen(screen_intro);
            controll::getScrManager()->activateScreen(screen_map);
            return true;
        }
        return false;
    }
};
//activate the context menu
class menuCaller : public eventHandler{
public:
    menuCaller(screen* source,  contextMenu* menu) : handled(source), menu(menu){}
    bool onEvent(SDL_Event* e){
        if(e->type == SDL_MOUSEBUTTONDOWN){
            if(menu->getVisible() == true){
                menu->setVisible(false);
                cout << "nope" << endl;
                return true;
            }
            menu->setWindowPosition(e->button.x, e->button.y, menu->getWidth(), menu->getHeight());
            menu->setVisible(true);
            cout << "yeah\n";
            return true;
        }
        return false;
    }
private:
    screen* handled;
    contextMenu* menu;
};
//buttons====================

//loadScreens--------------------------
//main loader------
bool screenLoader::loadScreens(screenManager* manager) {
    if (manager == NULL) manager = controll::getScrManager();
    if (!loadTestSquarScreen(manager)){
        cout << "could not load the test square" << endl;
        return false;
    }
    if(!loadContextMenus(manager)){
        cout << "coud not load the context menu" << endl;
        return false;
    }
    if (!loadIntroScreen(manager)) {
        cout << "could not load intro screen" << endl;
        return false;
    }
    if (!loadLoaderScreen(manager)) {
        cout << "could not load loader screen" << endl;
        return false;
    }
    if (!loadMapScreen(manager)) {
        cout << "could not load map screen" << endl;
        return false;
    }
    if (controll::getScrManager()->activateScreen(screen_intro)) {
        cout << "activated intro screen" << endl;
        cout.flush();
        return true;
    } else {
        cout << "could not activate intro screen" << endl;
        cout.flush();
        return false;
    }
    return true;
}
//screens----------
bool screenLoader::loadIntroScreen(screenManager* manager) {
    //var blok-----
    screen* tempScr = new screen();
    image* tempImage;
    //-------------
    //make screen
    if (tempScr == NULL) return false;
    //add on a child
    tempScr->addChild(manager->getScreen(screen_testsquare), 10, 10, 40, 40);
    //logo image
    tempImage = new PNGimage("./assets/guiGFX/logo.png");
    if (tempImage == NULL) return false;
    tempScr->addImage(tempImage, 20, 20);
    //make ng button
    tempImage = new PNGimage("./assets/guiGFX/button.png");
    if (tempImage == NULL) return false;
    tempImage->addEventHandler(new ngb());
    tempScr->addImage(tempImage, 20, 400);
    tempScr->addEventHandler(tempImage);
    //make visible
    tempScr->setVisible(true);
    //register screen
    manager->registerScreen(screen_intro, tempScr);
    return true;
}
bool screenLoader::loadLoaderScreen(screenManager* manager) {
    return true;
}
bool screenLoader::loadMapScreen(screenManager* manager) {
    //set up grid screen
    gridScreen* temp = new gridScreen();
    manager->registerScreen(screen_map, temp);
    temp->setLines(40, 40);
    temp->setHighlightLineSpacing(10);
    temp->setHighlightLineRGB(0, .5, .3);
    temp->setLineRGB(.4, .4, 1);

    double* t = new double[4];
    t[0] = 18.0;
    t[1] = 10.0;
    t[2] = 2.0;
    t[3] = 10.0;

    temp->setLineDash(t, 4);
    temp->setHighLinesDash(NULL, 0);

    //tack on  the menu
    screen* cm = manager->getScreen(screen_unitmenu);
    temp->addChild(cm, 0, 0, cm->getWidth(), cm->getHeight());
    menuCaller* mh = new menuCaller(temp, (contextMenu*) cm);
    temp->addEventHandler(mh);

    //set up vect images
    VECTimage* v = new VECTimage();
    v->addVertex(0, 0);
    v->addVertex(100, 100);
    v->addVertex(0, 100);
    v->setFill(true);
    v->setColor(1, 0, 0);
    v->setVisible(true);

    if (v == NULL) {
        return false;
    }
    temp->addImage(v, 100, 100);
    return true;
}
bool screenLoader::loadTestSquarScreen(screenManager* manager){
    //varblok------
    screen* testscreen;
    //varblok======

    //testsq-------
    testscreen = new screen(40, 40);
    testscreen->setBackground(255, 255, 0);
    manager->registerScreen(screen_testsquare, testscreen);
    testscreen->setVisible(true);
    //testsq=======

    return true;
}
bool screenLoader::loadContextMenus(screenManager* manager){
    //varblok------
    contextMenu* temp;
    string* strTemp;
    //varblok======
    
    //unitMenu-----
    strTemp = new string[4];
    strTemp[0] = "Move";
    strTemp[1] = "BLANK";
    strTemp[2] = "BLANK";
    strTemp[3] = "BLANK";
    temp = new contextMenu(contexttag_unitmenu, strTemp, 4);
    temp->setVisible(false);
    delete []strTemp;
    manager->registerScreen(screen_unitmenu, temp);
    //unitMenu=====

    return true;
}
//loadScreens==========================